package com.crape.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crape.account.bo.IAccountBO;
import com.crape.account.bo.IDarrenAuditBO;
import com.crape.account.bo.IDarrenIndexBO;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.DarrenAudit;
import com.crape.common.utils.PageUtils;

@Controller
public class AdminDarrenController {

    private IDarrenAuditBO darrenAuditBOImpl;
    private IAccountBO     accountBOImpl;
    private IDarrenIndexBO darrenIndexBOImpl;

    @Autowired
    public void setDarrenAuditBOImpl(IDarrenAuditBO darrenAuditBOImpl) {
        this.darrenAuditBOImpl = darrenAuditBOImpl;
    }
    
    @Autowired
    public void setAccountBOImpl(IAccountBO accountBOImpl) {
        this.accountBOImpl = accountBOImpl;
    }
    
    @Autowired
    public void setDarrenIndexBOImpl(IDarrenIndexBO darrenIndexBOImpl) {
		this.darrenIndexBOImpl = darrenIndexBOImpl;
	}

	@RequestMapping("/admin/darren/auditList.jsp")
    public String auditList(ModelMap modelMap, Long accountid, String email, Integer status, Integer pageNo) {
        modelMap.put("accountid", accountid);
        modelMap.put("email", email);
        modelMap.put("status", status);
        if (status == null) {
            status = 0;
        }
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        Integer pageSize = 20;
        Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
        Long counts = darrenAuditBOImpl.countDarrenAudits(accountid, email, status);
        modelMap.put("pageNo", pageNo);
        modelMap.put("counts", counts);
        if (counts == null || counts == 0) {
            modelMap.put("pageNum", 0);
            modelMap.put("darrenAuditList", null);
            return "/admin/darren/auditList.html";
        }
        modelMap.put("pageNum", PageUtils.getPagedNum(counts, pageSize));
        List<DarrenAudit> darrenAuditList = darrenAuditBOImpl.getPagedDarrenAudits(accountid, email, status, startIndex,
                                                                                  pageSize);
        modelMap.put("darrenAuditList", darrenAuditList);
        return "/admin/darren/auditList.html";
    }

    @RequestMapping("/admin/darren/audit.jsp")
    public String audit(ModelMap modelMap, Long auditid, Integer auditstatus, Long accountid, String email,
                        Integer status, Integer pageNo) {
        if (auditid == null || auditstatus == null) {
            return this.auditList(modelMap, accountid, email, status, pageNo);
        }
        darrenAuditBOImpl.updateDarrenAuditStatus(auditid, auditstatus);
        return this.auditList(modelMap, accountid, email, status, pageNo);
    }
    
    @RequestMapping("/admin/darren/list.jsp")
    public String list(ModelMap modelMap, Long accountid, String email, Integer indexType, Integer pageNo) {
        modelMap.put("accountid", accountid);
        modelMap.put("email", email);
        modelMap.put("indexType", indexType);
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        Integer pageSize = 20;
        Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
        Long counts = darrenIndexBOImpl.countIndexDarrens(accountid, email, indexType);
        modelMap.put("pageNo", pageNo);
        modelMap.put("counts", counts);
        if (counts == null || counts == 0) {
            modelMap.put("pageNum", 0);
            modelMap.put("darrenList", null);
            return "/admin/darren/listDarren.html";
        }
        modelMap.put("pageNum", PageUtils.getPagedNum(counts, pageSize));
        List<AccountInfo> darrenList = darrenIndexBOImpl.getIndexDarrens(accountid, email, indexType, startIndex, pageSize);
        modelMap.put("darrenList", darrenList);
        return "/admin/darren/listDarren.html";
    }
    
    @RequestMapping("/admin/darren/recommendDarren.jsp")
    public String recommendDarren(ModelMap modelMap, Long updateAccountid, Integer updateIndexType, 
    								 Long accountid, String email, Integer indexType, Integer pageNo) {
        darrenIndexBOImpl.recommendDarren(updateAccountid, updateIndexType);
        return this.list(modelMap, accountid, email, indexType, pageNo);
    }

}
